package com.yx.modules.api.controller;

import com.yx.common.annotation.Log;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.system.entity.TdBanner;
import com.yx.system.entity.TdCategory;
import com.yx.system.entity.TdOnlinePayLog;
import com.yx.system.service.TdBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * BannerController
 * Created by wuye on 2017/12/18.
 */
@RestController
@RequestMapping("/api/banner")
public class TdBannerConroller {

    @Autowired
    private TdBannerService tdBannerService;

    /**
     * 按ID查询bannaer
     *
     * @return
     */
    @PostMapping("findById")
    public R findById(int id) {
        return R.ok().put("tdBanner", tdBannerService.selectByPrimaryKey(id));
    }

    /**
     * 查询所有
     *
     * @param map
     * @return
     */
    @AuthIgnore
    @PostMapping("findAllByCondition")
    public R findAllByCondition(@RequestParam Map<String, Object> map) {
        Query query = new Query(map);
        List<TdBanner> tdBannerList = tdBannerService.queryList(query);
        int total = tdBannerService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tdBannerList, total, query.getLimit(), query.getPage());
        return R.ok().put("TdCategoryList", pageUtil);
    }


    /**
     * 新增或修改
     *
     * @return
     */
    @Log
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(TdBanner tdBanner) {
        if (tdBanner.getId() == 0 || tdBanner.getId() == null) {
            tdBannerService.insert(tdBanner);
        } else {
            tdBannerService.updateByPrimaryKey(tdBanner);
        }
        return R.ok();
    }

    /**
     * 删除 (软删除)
     *
     * @return
     */
    @Log
    @PostMapping("delete")
    public R delete(int id) {
        tdBannerService.deleteByPrimaryKey(id);
        return R.ok();
    }
}
